import {defineStore} from "pinia";
import {OaTabBarType} from "@/enum";

export const OA_ACCESS_TOKEN = 'OA_ACCESS_TOKEN';
export const OA_REFRESH_TOKEN = 'OA_REFRESH_TOKEN';
export const OA_TENANT_ID = '1'

type OaAppState = {
  type: string,
  code: string,
  state: string,
  tenantName: string,
  tabKey: OaTabBarType;
  userInfo: OaUserInfo
}

const OaAppState: OaAppState = {
  tenantName: '华信纵横',
  type: '35',
  code: '',
  state: '',
  tabKey: OaTabBarType.TODO,
  userInfo: {}
}

export const useOaAppStore = defineStore('OaApp', {
  state: (): OaAppState => ({
    ...OaAppState
  }),
  actions: {
    setTabKey(val: OaTabBarType): void {
      this.tabKey = val;
    },
    setOaToken(token: string) {
      uni.setStorageSync(OA_ACCESS_TOKEN, token);
    },
    setOaRefreshToken(token: string) {
      uni.setStorageSync(OA_REFRESH_TOKEN, token);
    },
    getToken(onlyToken: boolean = true) {
      const storageData = uni.getStorageSync(OA_ACCESS_TOKEN)
      const data = storageData ? JSON.parse(storageData) : null;
      if (data && onlyToken) {
        return data.v;
      }
      return data;
    },
    getOaRefreshToken() {
      return uni.getStorageSync(OA_REFRESH_TOKEN);
    },
    login() {
    }
  },
  persist: true,
})
